class LimitedStack:
    def __init__(self, maxsize):
        self.__maxsize = maxsize
        self.__stack = []
        self.__count = 0
        
    def push(self, elem):
        # Make room for this element.
        if self.__count == self.__maxsize:
            self.__stack.pop(0)
            self.__count -= 1
        # Insert the new element.
        self.__stack.append(elem)
        self.__count += 1
        
    def pop(self):
        if self.__count > 0:
            self.__count -= 1
            return self.__stack.pop()
        else:
            return None

    def clear(self):
        self.__stack = []
        self.__count = 0
        
    def empty(self):
        return self.__count == 0
    